﻿//==============================================================================  
//Copyright (C) 2012-2015 9UN.ORG. All rights reserved. 
//GUID：5d0f8a96-a85d-4a05-b28d-52690432dc71
//CLR Version: 4.0.30319.18033
//Code Author：Kevin Wang
//Contact：Email(Admin@9un.org),QQ(265382 or 74344)
//Filename：WordRender
//Namespace：RuanRang.Core.Renders
//Functions：WordRender  
//Created by Kevin Wang at 2013/2/3 10:57:56 http://blog.9un.org
//============================================================================== 

using System;
using System.Drawing;
using System.IO;
using System.Reflection;

namespace RuanRang.Core.Render
{
    public class WordRender : DocumentRender
    {
        /// <summary>
        /// Gets or sets the type of the image save options.
        /// </summary>
        /// <value>The type of the image save options.</value>
        public Type ImageSaveOptionsType { get; protected set; }

        /// <summary>
        /// Gets or sets the type of the save format.
        /// </summary>
        /// <value>The type of the save format.</value>
        public Type SaveFormatType { get; protected set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="WordRender" /> class.
        /// </summary>
        /// <param name="container">The container.</param>
        public WordRender(RenderPluginContainer container)
        {
            TargetType = container.Resolve(RenderPluginType.Word, "Aspose.Words.Document");
            ImageSaveOptionsType = container.Resolve(RenderPluginType.Word, "Aspose.Words.Saving.ImageSaveOptions");
            SaveFormatType = container.Resolve(RenderPluginType.Word, "Aspose.Words.SaveFormat");
        }

        /// <summary>
        /// Opens the specified file path.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        public override void Open(string filePath)
        {
            TargetFile = Activator.CreateInstance(TargetType, new object[] { filePath });
            var property = TargetType.GetProperty("PageCount", BindingFlags.Public | BindingFlags.GetProperty | BindingFlags.Instance);
            PageCount = (int)property.GetValue(TargetFile, null);
        }

        /// <summary>
        /// Gets the image.
        /// </summary>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="resolution">The resolution.</param>
        /// <returns>Bitmap.</returns>
        public override Image GetImage(int pageIndex, float resolution = 100)
        {
            var imageType = Enum.Parse(SaveFormatType, "Jpeg");
            var saveInfo = Activator.CreateInstance(ImageSaveOptionsType, new[] { imageType });
            const BindingFlags isotBindingFlags = BindingFlags.Public | BindingFlags.GetProperty |
                                                  BindingFlags.Instance;
            var pageCountproperty = ImageSaveOptionsType.GetProperty("PageCount", isotBindingFlags);
            var pageIndexproperty = ImageSaveOptionsType.GetProperty("PageIndex", isotBindingFlags);
            var jpegQualityproperty = ImageSaveOptionsType.GetProperty("JpegQuality", isotBindingFlags);
            //页面数量
            pageCountproperty.SetValue(saveInfo, 1, null);
            //页面索引
            pageIndexproperty.SetValue(saveInfo, pageIndex, null);
            //设置图片质量
            jpegQualityproperty.SetValue(saveInfo, 100, null);
            var docStream = new MemoryStream();
            const BindingFlags bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.InvokeMethod;
            TargetType.InvokeMember("Save", bindingFlags, Type.DefaultBinder, TargetFile,
                                              new[] { docStream, saveInfo });
            var image = Image.FromStream(docStream);
            var result = image.Clone() as Image;
            image.Dispose();
            return result;
        }
    }
}
